package com.springsummer.information.green.beans;

import com.alibaba.fastjson.JSON;
import com.springsummer.information.green.hello.*;
import com.springsummer.information.green.hello.HelloOneService;

public class ClassPathXmlApplicationContextTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("springmini-one.xml");
        //

        HelloOneService helloOneService= (HelloOneService) applicationContext.getBean("helloOneService");
        System.out.println("isSingleton = " + applicationContext.isSingleton("helloOneService"));
        helloOneService.sayHello();


        HelloOneService helloOneServiceTwo= (HelloOneService) applicationContext.getBean("helloOneService");
        helloOneServiceTwo.sayHello();

        HelloTwoService helloTwoService=(HelloTwoService) applicationContext.getBean("helloTwoService");
        helloTwoService.helloTwo();
        System.out.println();

        //EmpRepository empRepository=(EmpRepository) beanFactory.getBean("empRepository");

        HelloEmpService helloEmpService=(HelloEmpService) applicationContext.getBean("helloEmpService");
        String helloEmpRes=helloEmpService.addEmp("pingping");
        System.out.println(helloEmpRes);
        System.out.println();

        System.out.println("cycle depency");
        System.out.println();

        //cycle
        OneService oneService= (OneService) applicationContext.getBean("oneService");
        oneService.sayHello("pingping");
        System.out.println();

        TwoService twoService=(TwoService) applicationContext.getBean("twoService");
        twoService.sayHello("spring");
        System.out.println();

        ThreeService threeService=(ThreeService)applicationContext.getBean("threeService");
        threeService.sayHello("lucy");
        System.out.println();

        FruitInitOne fruitInitOne= (FruitInitOne) applicationContext.getBean("fruitInitOne");
        System.out.println("FruitInitOne = " + JSON.toJSONString(fruitInitOne));
        System.out.println();

        //add AutowiredBeanPostProcessor
        System.out.println("AutowiredAnnotationBeanPostProcessor");


        AutoEmpService autoEmpService= applicationContext.getBean("autoEmpService",AutoEmpService.class);
        autoEmpService.addEmp("lucy");

        System.out.println();
        //beanFactory.getBean("hi");

        System.out.println();
        System.out.println("ApplicationContext");

        System.out.println(applicationContext.getApplicationName());
        NormalEnvironmentImpl environment= (NormalEnvironmentImpl) applicationContext.getEnvironment();
        for(String propName:environment.getPropertyNames()){
            System.out.println(propName+"=" + applicationContext.getEnvironment().getProperty(propName));
        }
        System.out.println();

        System.out.println();



    }

}
